// 混入tabBar购物车的徽标的设置
import {mapGetters} from 'vuex'
export default {
  data(){
    return {
      // goodsTotal: 0
    }
  },
  computed:{
    ...mapGetters(['goodsInfo','countAll'])
  },
  onShow(){
    // 设置徽标
    this.setBudge(this.countAll)
  },
  methods: {
    // 设置tabBar栏的徽标
    setBudge(val){
      uni.setTabBarBadge({
        index: 2,
        text: val + ''
      })
    },
    // 计算购物车商品总数
    getTotal(){
      // let total = 0
      // 过滤出goods_status的值为true的商品
      /* this.goodsInfo.filter(obj => obj.goods_status === true).forEach(item =>{
        total += item['goods_total']
      }) */
      this.goodsTotal = this.goodsInfo.length
      return this.goodsInfo.filter(obj => obj.goods_status === true).reduce((total,item)=>total+=item.goods_total,0) + ''
      // total += ''
      // return total
    }
  },
  watch:{
    countAll:{
      handler(newVal){
        // console.log('tabBar',newVal);
        this.setBudge(newVal)
      },
      immediate: true
    }
  }
}